<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
//use GuzzleHttp\Client;

class TokenController extends Controller
{


    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function getToken(Request $request)
    {
//        phpinfo();

        //
//        $oauthUrl = 'http://elsf.lan6.oyfei.cn';
        $oauthUrl = $_SERVER['HTTP_HOST'];
//        $userName = 'yafei01@qq.com';
        $userName = $request->get('username');
//        $userName = 'yafei01';
//        $password = '123456';
        $password = $request->get('password');
        $clientId = env('PASSPORT_CLIENT_ID','1');
        $clientSecret = env('PASSPORT_CLIENT_SECRET','nKINzXLXdNB4OPWZMnGykRSCsD2VfIz6F2sr5qWP');
//var_dump($clientSecret);

        $http = new \GuzzleHttp\Client;

        $response = $http->post($oauthUrl.'/oauth/token', [
            'form_params' => [
                'grant_type' => 'password',
                'client_id' => $clientId,
                'client_secret' => $clientSecret,
                'username' => $userName,
                'password' => $password,
                'scope' => '',
            ],
        ]);
//        dd($response);
        $resBody = json_decode((string) $response->getBody(), true);
//        return json_decode((string) $response->getBody(), true);

        $info['code'] = '0';
        $info['msg'] = '成功';
        $info['data'] = [
            'token_type' => $resBody['token_type'], //
            'expires_in' => $resBody['expires_in'], //失效时间
            'access_token' => $resBody['access_token'], //
            'refresh_token' => $resBody['refresh_token']
        ];

        return $info;



    }
}
